package com.example.list.jetpack;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.work.Data;
import androidx.work.Worker;
import androidx.work.WorkerParameters;

public class MyWork extends Worker {

    private Context mContext;
    //任务携带参数
    private WorkerParameters workerParameters;

    public MyWork(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
        this.mContext = context;
        this.workerParameters = workerParams;
    }

    @NonNull
    @Override
    public Result doWork() {
        //接受传第过来的参数 这里的data与MainActivity的data 需要一致
        String data = workerParameters.getInputData().getString("data");

        //返回处理的结果
        Data outData = new Data.Builder().putString("result","来自worker处理后的结果").build();
        @SuppressLint("RestrictedApi")
        Result.Success success = new Result.Success(outData);
        return success;

//        版权声明：本文为CSDN博主「SYOICQ」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
//        原文链接：https://blog.csdn.net/sunlifeall/article/details/114828655
    }
}
